//index.js
//获取应用实例
const app = getApp();
var OPEN_ID = wx.getStorageSync('wx_open_id');//储存获取到openid  
var SESSION_KEY = '';//储存获取到session_key  
const user = wx.getStorageSync("user");
const user_token = (wx.getStorageSync('user_token'));
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');
Page({
  scanningCode: function (event) {
//  wx.redirectTo({
//           url: '../ad/index'
//         })


    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        console.log(res)
        var src = res.result;
        src = src.substr(52);
        this.topage(src);


        // wx.redirectTo({
        //   url: '../carInfo/index?q=' + src
        // })
      }
    })
  },
  callPhone: function (event) {
    wx.makePhoneCall({
      phoneNumber: kfrx
    })
  },
  onLoad: function (options) {
    // var that = this;
    // var id = decodeURIComponent(options.q);

    //   var regNum = new RegExp('[0-9]', 'g');
  
    //   if (regNum.exec(id)) {
    //     id = id.substr(52);
        
    //     this.topage(id)
    //   }
      

  },

  topage: function (id) {
    var user_token = (wx.getStorageSync('user_token'))
    wx.showLoading({
      title: '加载中',
      success: () => {
        wx.request({
          url: base_url + '/Qcode/index',
          method: 'POST',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'id': id
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            console.log(res)
            if (res.data.success == '1') {
              if (res.data.code == '0') {
                wx.navigateTo({
                  url: '../carInfo/index?id=' + id
                })
              } else if (res.data.code == '800') {
                wx.navigateTo({
                  url: '../bindPhone/index?id=' + id
                })
              } else if (res.data.code == '801') {
                wx.navigateTo({
                  url: '../myInfo/index'
                })
              }
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  }
})
